package com.fd.servlet;

import com.fd.service.impl.StudentServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Service
public class LoginServlet extends HttpServlet {
//    final String USERNAME="sss";
//    final String PASSWORD="123";
//    @Autowired
//    private IStudentService studentService;
//    IStudentService studentService = new StudentService();
    public LoginServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentServiceImpl studentService = getiStudentService();

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username.equals(username)&&password.equals(password)){
//            ITestJDBC testJDBC = new TestJDBC();
            req.setAttribute("student", studentService.getAll());
            req.getRequestDispatcher("/data").forward(req,resp);
        }else {
            resp.getWriter().write("false");
        }
    }

    private static StudentServiceImpl getiStudentService() {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext-jdbc-ann.xml");
        StudentServiceImpl studentService = classPathXmlApplicationContext.getBean(StudentServiceImpl.class);
        return studentService;
    }
}